#include "model.h"

#include <fstream>
#include <gl/glew.h>
#include <stdlib.h>

struct Vert{
    Vert(){ x=y=z=0; }
    Vert(float x, float y, float z):x(x),y(y),z(z){}
    float x, y, z;
};

struct Face{
    Face(int ia, int ib, int ic):ia(ia),ib(ib),ic(ic){}
    int ia, ib, ic; //indices
};

Model::Model(std::string fileLocation){
    float a,b,c;
    char ctemp;
    std::ifstream file;
    std::string line;
    std::vector<int> indices;
    std::vector<int> uvindices;

    std::vector<Vert> verts;
    std::vector<Face> faces;

    file.open(fileLocation.c_str());
    if(file.is_open()==false){
        std::cout<<"Unable to open model: '"<<fileLocation<<"'"<<std::endl;
        // @TODO make it load a cube
        return;
    }

    while(file.good()){
        getline(file, line);

        if(line[0]=='v' and line[1]==' '){
            sscanf (line.c_str(), "%c %f %f %f", &ctemp, &a, &b, &c);
            verts.push_back(Vert(a, b, c));
        }
        else if(line[0]=='v' and line[1]=='n'){
            sscanf (line.c_str(), "%c%c %f %f %f", &ctemp, &ctemp, &a, &b, &c);
//            rawNormals.push_back(a);
//            rawNormals.push_back(-c);
//            rawNormals.push_back(b);
        }
        else if(line[0]=='v' and line[1]=='t'){
            sscanf (line.c_str(), "%c%c %f %f", &ctemp, &ctemp, &a, &b);
//            rawUVCoords.push_back(a);
//            rawUVCoords.push_back(b);
        }

        else if(line[0]=='f' and line[1]==' '){// first 0,3,6 are vertexIndices, 1,4,7 are texture, 2,5,8 are vertnorm, k is the variable
            int iverts[3];
            int begin = 0;
            int len = 0;
            for(unsigned int i=0;i<3;i++){
                begin = line.find(' ', begin)+1;
                len = line.find('/', begin)-begin;
                iverts[i] = atoi(line.substr(begin, len).c_str());
            }
            faces.push_back(Face(iverts[0]-1, iverts[2]-1, iverts[1]-1));

            begin = 0;
            len = 0;
            for(unsigned int i=0;i<3;i++){
                begin = line.find(' ', begin)+1;
                begin = line.find('/', begin)+1;
                len = line.find('/', begin)-begin;
                iverts[i] = atoi(line.substr(begin, len).c_str());
            }
            uvindices.push_back(iverts[0]-1);
            uvindices.push_back(iverts[2]-1);
            uvindices.push_back(iverts[1]-1);
        }
    }
    std::cout<<"Sizeof faces: "<<faces.size()<<std::endl;
    for(auto face:faces){
        vertices.push_back(verts.at(face.ia).x);
        vertices.push_back(verts.at(face.ia).y);
        vertices.push_back(verts.at(face.ia).z);
        vertices.push_back(1);
        vertices.push_back(verts.at(face.ib).x);
        vertices.push_back(verts.at(face.ib).y);
        vertices.push_back(verts.at(face.ib).z);
        vertices.push_back(1);
        vertices.push_back(verts.at(face.ic).x);
        vertices.push_back(verts.at(face.ic).y);
        vertices.push_back(verts.at(face.ic).z);
        vertices.push_back(1);
//        std::cout<<"Face: "<<face.ia<<", "<<face.ib<<", "<<face.ic<<std::endl;
    }
    std::cout<<"Vertices:\n";
    for(auto vert:vertices){
        std::cout<<vert<<", ";
    }
    file.close();
//    std::cout<<"Vertices size: "<<rawVertices.size()<<std::endl;
    std::cout<<"Vertices size: "<<vertices.size()<<std::endl;
//    std::cout<<"Number of vertices: "<<
//    GLuint textureid = LoadTexture(fileLocation+".png");
//    std::cout<<"Loaded texture ID: "<<textureid<<std::endl;
//    SetTexture(textureid);
}


void Model::AddUV(int a, int b){
    uvcoords.push_back(Math::Vertex(a, b, 0));
}

float* Model::GetVertexData(){
    return &vertices[0];
}
